<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.hosname" placeholder="医院名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.hoscode" placeholder="医院编号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getHospitalSets">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="hospitalSets" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
      style="width: 100%">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="hosname" label="医院名称" width="150" sortable>
      </el-table-column>
      <el-table-column prop="hoscode" label="医院编号" width="150" sortable>
      </el-table-column>
      <el-table-column prop="apiUrl" label="api基础路径" width="220" sortable>
      </el-table-column>
      <el-table-column prop="contactsName" label="联系人姓名" width="130" sortable>
      </el-table-column>
      <el-table-column prop="contactsPhone" label="联系人手机" min-width="140" sortable>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120" :formatter="formatStatus" sortable>
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)" title="编辑"><i class="el-icon-edit"></i>
          </el-button>
          <el-button type="danger" size="small" title="删除" @click="handleDel(scope.$index, scope.row)"><i
              class="el-icon-delete"></i></el-button>
          <el-button size="small" type="warning" icon="el-icon-lock" @click="lockHospSet(scope.row.id,0)" title="锁定"
            v-if="scope.row.status==1"></el-button>
          <el-button size="small" type="info" icon="el-icon-unlock" @click="lockHospSet(scope.row.id,1)" title="取消锁定"
            v-if="scope.row.status==0"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="page"
        :page-size="limit" layout="prev, pager, next, jumper" :total="total" style="float:right">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
        <el-form-item label="医院名称" prop="hosname">
          <el-input v-model="editForm.hosname" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="医院编号" prop="hoscode">
          <el-input v-model="editForm.hoscode" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="api基础路径" prop="apiUrl">
          <el-input v-model="editForm.apiUrl" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactsName">
          <el-input v-model="editForm.contactsName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人号码" prop="contactsPhone">
          <el-input v-model="editForm.contactsPhone" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import apiObj from '../../api/hospSet/list'

export default {
  data () {
    return {
      filters: {
        hoscode: '',
        hosname: ''
      },
      hospitalSets: [],
      total: 0,
      page: 1,
      limit: 5,
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        hosname: [
          { required: true, message: '请输入医院名称', trigger: 'blur' }
        ],
        hoscode: [
          { required: true, message: '请输入医院编号', trigger: 'blur' }
        ],
        apiUrl: [
          { required: true, message: '请输入api基础路径', trigger: 'blur' }
        ],
        contactsName: [
          { required: true, message: '请输入联系人姓名', trigger: 'blur' }
        ],
        contactsPhone: [
          { required: true, message: '请输入联系人号码', trigger: 'blur' }
        ],
      },
      //编辑界面数据
      editForm: {},
    }
  },
  methods: {
    formatStatus: function (row, column) {
      return row.status == 1 ? '可用' : row.status == 0 ? '不可用' : '未知'
    },
    handleCurrentChange (val) {
      this.page = val
      this.getHospitalSets()
    },
    handleSizeChange (val) {
      this.limit = val
      this.getHospitalSets()
    },
    //获取医院设置列表
    getHospitalSets () {
      let para = {
        hosname: this.filters.hosname,
        hoscode: this.filters.hoscode
      }
      this.listLoading = true
      apiObj.getHospitalSetListPage(this.page, this.limit, para).then(({ data }) => {
        this.total = data.data.total
        this.hospitalSets = data.data.records
        this.listLoading = false
      })
    },
    //删除
    handleDel (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        let para = row.id
        apiObj.removeHospitalSet(para).then(({ data }) => {
          if (data.code == 200) {
            this.listLoading = false
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getHospitalSets()
          } else {
            this.$message.error("删除失败!")
          }

        })
      }).catch(() => {
        this.$message("已取消删除")
      })
    },
    //显示编辑界面
    handleEdit (index, row) {
      this.editFormVisible = true
      this.editForm = Object.assign({}, row)
    },
    //编辑
    editSubmit () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗?', '提示', {}).then(() => {
            this.editLoading = true
            let para = Object.assign({}, this.editForm)
            apiObj.editHospitalSet(para).then((res) => {
              this.editLoading = false
              this.$message({
                message: '提交成功',
                type: 'success'
              })
              this.$refs['editForm'].resetFields()
              this.editFormVisible = false
              this.getHospitalSets()
            }).catch((e) => {
              this.$message.error("出现错误，稍后再试！")
            })
          })
        }
      })
    },
    selsChange (sels) {
      this.sels = sels
    },
    //批量删除
    batchRemove () {
      var ids = this.sels.map(item => item.id).toString()
      this.$confirm('确认删除选中记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        let para = ids
        apiObj.batchRemoveHospitalSet(para).then((res) => {
          this.listLoading = false
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getHospitalSets()
        }).catch((e) => {
          this.$message.error("操作失败！")
        })
      }).catch(() => {
        this.$message('操作取消')
      })
    },
    // 解锁和锁定的操作
    lockHospSet (id, status) {
      this.$confirm('确认执行该操作吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true

        apiObj.lockHospitalSet(id, status).then((res) => {
          this.listLoading = false
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.getHospitalSets()
        })
      }).catch(() => {
        this.$message('操作取消')
      })
    }
  },
  created () {
    this.getHospitalSets()
  }
}

</script>

<style>
</style>